/***********************************************************************************************************************
* Copyright (C) . All rights reserved.
***********************************************************************************************************************/

/***********************************************************************************************************************
* File Name    : main.c
* Version      :  
* Device(s)    : BAT32G133
* Tool-Chain   : MDK(armcc)
* Description  : This file is a template.
* Creation Date: 2019/4/30
***********************************************************************************************************************/

/***********************************************************************************************************************
Macro Definitions
***********************************************************************************************************************/
#define TRMW   *((volatile uint8_t *)(0x40021C08))
#define TRMR   *((volatile uint8_t *)(0x40021C0C))
#define TRMT   *((volatile uint8_t *)(0x40021C10))
//#define SOP28
//#define STOP_RESTORE_WITH_MOSC
	
/***********************************************************************************************************************
Includes
***********************************************************************************************************************/
#include <stdio.h>
#include "BAT32G137.h"
#include "userdefine.h"
#include "clk.h"
#include "gpio.h"
#include "key.h"
#include "rtc.h"

/***********************************************************************************************************************
Global variables and functions
***********************************************************************************************************************/
volatile uint32_t g_ticks;

void delayMS(uint32_t n)
{
		g_ticks = n;
		while(g_ticks);
}

int main()
{
	uint32_t msCnt; 	// count value of 1ms
	uint32_t i;

//-----------------------------------------------------------------------
// Change wakeup time 
//-----------------------------------------------------------------------	
//	TRMT = 0x10; 	
//	TRMW = 0x1F;
	
//-----------------------------------------------------------------------
// OSC enable 
//-----------------------------------------------------------------------
#ifdef STOP_RESTORE_WITH_MOSC
	CGC->OSTS = 0x00;  // MOSC Stable time
	CLK_Osc_Setting(OSC_OSCILLATOR, OSC_OSCILLATOR);	
	CLK_Fclk_Select(MAINCLK_FMX);
#else
  //CLK_SubOsc_Setting(OSC_OSCILLATOR, OSC_LOW_POWER);
	CLK_SubOsc_Setting(OSC_OSCILLATOR, OSC_NORMAL_POWER);
#endif
	
//-----------------------------------------------------------------------
// PCLBUZ0 
//-----------------------------------------------------------------------
	CLKBUZ0_PORT_SETTING();
	PCBZ->CKS0  = 0x83;    	// FMAIN/2^3	

//-----------------------------------------------------------------------
// PCLBUZ1 output 
//-----------------------------------------------------------------------
#ifdef SOP28  
  PORT->PM1   &= ~(1<<5); // P15/PCLBUZ1
#else
  PORT->PM14  &= ~(1<<1); // P141/PCLBUZ1
#endif
  PCBZ->CKS1  = 0x8E;    	// FSUB/2^6
	
//-----------------------------------------------------------------------
// Enable KR0 falling edge interrupt request to wakeup system
//-----------------------------------------------------------------------   
	KEY_Init(1 << 0);               // Initializes P70/KR0
	KEY_Start();                    // Enable KEY Interrupt
	__disable_irq(); 	/* don't entry ISR when wake up by intp0 */

#if 1
//-----------------------------------------------------------------------
// RTC Constant Period Interrupt Demo (1 second)
//-----------------------------------------------------------------------
  RTC_Init(RTC_FSUB);
  RTC_Start();
  date_time.year  = 0x19;
  date_time.month = 0x06;
  date_time.week  = SATURDAY; 
  date_time.day   = 0x01;
  date_time.hour  = 0x08;
  date_time.min   = 0x30;
  date_time.sec   = 0x00;
  RTC_Set_CounterValue(&date_time);
  RTC_Set_ConstPeriodInterruptOn(ONESEC);
	RTC_1HZ_Output();
#endif

//-----------------------------------------------------------------------
// P72: Wakeup Flag
//-----------------------------------------------------------------------	
	GPIO_Output_Enable(&PORT->P7, 1<<2);	
	
//-----------------------------------------------------------------------
// wait a moment
//-----------------------------------------------------------------------
	for(i = 0; i < 100000; i++) ;
	
	while(1U)
	{		
	//-----------------------------------------------------------------------
	// Stop Entry
	//-----------------------------------------------------------------------	
		__STOP();
		
	//-----------------------------------------------------------------------
	// Stop Restore
	//-----------------------------------------------------------------------	
		PORT->P7 ^= (1<<2); /* P72 toggle */

		INTC_ClearPendingIRQ(RTC_IRQn);
		NVIC_ClearPendingIRQ(RTC_IRQn);		
		INTC_ClearPendingIRQ(KEY_IRQn);
		NVIC_ClearPendingIRQ(KEY_IRQn);
	}
	
}

/***********************************************************************************************************************
* Function Name: SysTick Handler
* Description  : Decreament the g_ticks value
* Arguments    : None
* Return Value : None
***********************************************************************************************************************/
void SysTick_Handler(void)
{
	g_ticks--;
}
